package com.bestcem.xm.user.grpc.service;

import com.bestcem.bp.xm.user.grpc.v1.services.ListOrgCustomSettingsRequest;
import com.bestcem.bp.xm.user.grpc.v1.services.ListOrgCustomSettingsResponse;
import com.bestcem.bp.xm.user.grpc.v1.services.OrgCustomSettingServiceGrpc;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.grpc.service.converter.OrganizationCustomSettingConvert;
import com.bestcem.xm.user.service.OrganizationCustomSettingService;
import com.bestcem.xm.user.service.dto.user.OrganizationCustomSettingDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 租户自定义配置grpc服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/10 13:38
 */
@Slf4j
@GrpcService
public class OrganizationCustomSettingGrpcService extends OrgCustomSettingServiceGrpc.OrgCustomSettingServiceImplBase {

    @Resource
    private OrganizationCustomSettingService organizationCustomSettingService;

    /**
     * 根据相关字段查询公司自定义配置信息列表
     *
     * @param request          请求信息
     * @param responseObserver 返回值
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/10 13:46
     */
    @Override
    public void listOrgCustomSettings(ListOrgCustomSettingsRequest request, StreamObserver<ListOrgCustomSettingsResponse> responseObserver) {
        // 参数转换
        OrganizationCustomSettingDTO settingDTO = OrganizationCustomSettingConvert.settingToDto(request);
        ListOrgCustomSettingsResponse.Builder builder = ListOrgCustomSettingsResponse.newBuilder();

        ServiceResult<List<OrganizationCustomSettingDTO>> serviceResult = organizationCustomSettingService.selectBySelective(settingDTO);
        if (serviceResult.isSuccess()) {
            //查询成功
            builder.setStatus(RpcStatusResult.success());
            builder.addAllSettings(OrganizationCustomSettingConvert.dtosToSettings(serviceResult.getData()));
        } else {
            //查询失败
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

}
